home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / MEMMON_F / MMAIN.C < prev    next >
C/C++ Source or Header  |  1990-03-02  |  4KB  |  161 lines

  1. /*
  2.  * mmain.c: code shared among memmon back ends.
  3.  */
  4.  
  5. /* in this file only, define globals instead of just declaring */
  6. #define Global
  7. #include "memmon.h"
  8.  
  9. hidden novalue mmoptions Params((int argc, char *argv[]));
  10. hidden novalue mmusage Params((noargs));
  11.  
  12. static int justcolors = 0;        /* quit after setting colors? */
  13. static int gcskip = 0;            /* number of gc's to skip */
  14.  
  15. /*
  16.  * main program.
  17.  */
  18. novalue main(argc, argv)
  19. int argc;
  20. char *argv[];
  21.    {
  22.    progname = argv[0];            /* save program name for diagnostics */
  23.    devsetup();                /* init device-dependent params */
  24.    initcolors();            /* init color tables */
  25.    mmoptions(argc, argv);        /* process command options */
  26.    devinit();                /* open and initialize device */
  27.  
  28.    if (justcolors) {            /* if just color setting wanted: */
  29.       setmap(Marked, C_Marked);            /* load color map */
  30.       mquit(NormalExit);            /* terminate */
  31.    }
  32.  
  33.    if (strcmp(whenpause,"") != 0 && strcmp(whenpause,"n") != 0 && !batchmode) {
  34.       ttyi = fopen("/dev/tty", "r");    /* init tty input file */
  35.       ttyo = fopen("/dev/tty", "w");    /* init tty output file */
  36.       if (ttyi == NULL || ttyo == NULL) {
  37.          fprintf(stderr, "%s: can't open /dev/tty; will not pause\n", progname);
  38.          ttyi = ttyo = NULL;
  39.          }
  40.       }
  41.  
  42.    skipgc(gcskip);            /* skip the first n collections */
  43.    memmon();                /* run the memory monitor */
  44.    mquit(NormalExit);            /* terminate */
  45.    }
  46.  
  47. /*
  48.  * mmoptions(argc, argv) - process command options.
  49.  */
  50. static novalue mmoptions(argc, argv)
  51. int argc;
  52. char *argv[];
  53.    {
  54.    int c;
  55.    extern char *optarg;    /* getopt() */
  56.    extern int optind;    /* getopt() */
  57.  
  58.    /*
  59.     * set some defaults
  60.     */
  61.    gclimit = -1;            /* no limit on gc's */
  62.    pauselimit = -1;            /* no limit on pauses */
  63.    whichregs = "sb";            /* regions */
  64.    if (batchmode)
  65.       whenpause = "fgacpd";        /* pause/print points */
  66.    else
  67.       whenpause = "fgacp";        /* pause/print points */
  68.  
  69.    /*
  70.     * We should ideally keep the next two lines in sync!
  71.     * Unfortunately, there are too many options to include all in synopsis.
  72.     */
  73. #define MMUse "[-r {fsb}] [-p {fgacpdn}] [-bwhLMgqQS n] [-t title] [...] [file]"
  74.    while ((c = getopt(argc, argv, "r:p:b:w:h:L:M:g:q:Q:S:t:mc:C:")) != EOF)
  75.       switch (c) {
  76.          case 'r':
  77.             whichregs = optarg;
  78.             break;
  79.          case 'p':
  80.             whenpause = optarg;
  81.             break;
  82.          case 'b':
  83.             granularity = atoi(optarg);
  84.             break;
  85.          case 'w':
  86.             width = atoi(optarg);
  87.             break;
  88.          case 'h':
  89.             height = atoi(optarg);
  90.             break;
  91.          case 'L':
  92.             textrow = atoi(optarg);
  93.             break;
  94.          case 'M':
  95.             memrow = atoi(optarg);
  96.             break;
  97.          case 'g':
  98.             gcskip = atoi(optarg);
  99.             break;
  100.          case 'q':
  101.             gclimit = atoi(optarg);
  102.             break;
  103.          case 'Q':
  104.             pauselimit = atoi(optarg);
  105.             break;
  106.          case 'S':
  107.             sfreq = atoi(optarg);
  108.             break;
  109.          case 't':
  110.             title = optarg;
  111.             break;
  112.          case 'm':
  113.             showmarking = 1;
  114.             break;
  115.          case 'c':
  116.             readcolors(optarg);
  117.             break;
  118.          case 'C':
  119.             justcolors = 1;
  120.             readcolors(optarg);
  121.             break;
  122.          default:
  123.             mmusage();
  124.          }
  125.  
  126.    if (index(whenpause, 'g') || index(whenpause, 'a'))
  127.       showmarking = 1;            /* show marking if going to pause */
  128.  
  129.    if (optind + 1 < argc)        /* if too many files named, quit */
  130.       mmusage();
  131.  
  132.    if (optind < argc) {            /* open input file, if specified */
  133.       ifile = fopen(argv[optind], "r");
  134.       if (!ifile)
  135.          pexit(argv[optind]);
  136.       if (!title)
  137.          title = argv[optind];        /* use file name as default title */
  138.       }
  139.    else                    /* else use standard input */
  140.       ifile = stdin;
  141.    }
  142.  
  143. /*
  144.  * mmusage() - diagnose bad memmon usage, and abort.
  145.  */
  146. static novalue mmusage()
  147.    {
  148.    fprintf(stderr, "usage: %s %s\n", progname, MMUse);
  149.    exit(ErrorExit);
  150.    }
  151.  
  152. /*
  153.  * mquit(exitcode) - terminate the run.
  154.  */
  155. novalue mquit(exitcode)
  156. int exitcode;
  157.    {
  158.    devterm();                /* close down the graphics device */
  159.    exit(exitcode);
  160.    }
  161.